/**
 * Created by Amy on 2018/8/2.
 */
var userNameOld = '';
var roleId = getCookie("roleId");

$(function () {
    isLoginFun();
    header();
    $("#ctl01_lblUserName").text(getCookie('userName'));
    createTimePicker();
    changeTitle();

});

// 判断是编辑还是创建
function changeTitle() {
    var userTitle = getCookie("userTitle");
    var text = "<h1 class=\"hd-title pull-left\">" + userTitle + "人员</h1>";
    var opusername = getCookie("opusername");
    $("#userTitle").append(text);
    if (userTitle === '创建') {
        $("#userName").removeAttr("disabled");
        $('#createBtn').val('立即创建');
        $('#noPassword').css('display', 'block');

        // 创建用户
        $('#createBtn').click(function () {
            var userName = $("#userName").val();
            //userName长度为6-20位
            var reg = /^[a-zA-Z0-9]{1,10}$/;
            if (!reg.test(userName)) {
                layer.msg('用户名长度为1-10位', {icon: 2});
                return;
            }
            var password = $("#password").val();
            if (!reg.test(password)) {
                layer.msg('密码长度为1-10位', {icon: 2});
                return;
            }
            // 获得手机号
            var phone = $("#phoneNumText").val();
            //验证手机号
            if (!(/^1[34578]\d{9}$/.test(phone))) {
                layer.msg("手机号码有误，请重填", {icon: 2});
                return ;
            }
            var timeRange = $("#config-demo").val();
            var startTime = dateChange(timeRange.split(' ~ ')[0]);
            var stopTime = dateChange(timeRange.split(' ~ ')[1]);
            if (userName === "") {
                layer.msg("账号不能为空", {icon: 2});
            } else if (password === "") {
                layer.msg("密码不能为空", {icon: 2});
            } else if (timeRange === "") {
                layer.msg("起止时间不能为空", {icon: 2});
            } else if (userName.length > 10 || password.length > 10) {
                layer.msg("账号或者密码最多不可超过10个字", {icon: 2});
            } else {
                var url = '/admin/addUserInfo';
                var data = {
                    "opusername": opusername,
                    "username": userName,
                    "password": password,
                    "startTime": startTime,
                    "stopTime": stopTime,
                    "roleId": "0",
                    "phoneNum": phone
                };
                commonAjaxPost(true, url, data, function (result) {
                    if (result.code === "666") {
                        layer.msg("用户创建成功，权限已开启", {icon: 1});
                        setTimeout(function () {
                            window.location.href = 'TenantsManage.html';
                        }, 1000)
                    } else if (result.code === "50003") {
                        //用户名已存在
                        layer.msg(result.message, {icon: 2});
                    } else if (result.code === "333") {
                        layer.msg(result.message, {icon: 2});
                        setTimeout(function () {
                            window.location.href = 'login.html';
                        }, 1000)
                    } else {
                        layer.msg(result.message, {icon: 2});
                    }
                })
            }
        })

    } else {
        $("#userName").removeAttr("disabled");
        // $("#userName").attr("disabled","disabled");
        $('#createBtn').val('保存修改');
        $('#noPassword').css('display', 'block');
        var userId = getCookie('userId');
        //console.log(userId);
        var url = '/admin/selectUserInfoById';
        var data = {
            'id': userId
        };
        commonAjaxPost(true, url, data, function (result) {
            if (result.code === "666") {
                var userInfo = result.data;
                //console.log(userInfo);
                userNameOld = $("#userName").val(userInfo.username);
                $("#password").val(userInfo.password);
                $("#phoneNumText").val(userInfo.phone_num);
                $("#config-demo").val(
                    userInfo.start_time.replace(/-/g, '/').replace(/T/g, ' ') + " ~ "
                    + userInfo.stop_time.replace(/-/g, '/').replace(/T/g, ' '));
            } else if (result.code === "333") {
                layer.msg(result.message, {icon: 2});
                setTimeout(function () {
                    window.location.href = 'login.html';
                }, 1000)
            } else {
                layer.msg(result.message, {icon: 2})
            }
        })

        $('#createBtn').click(function () {
            var userName = $("#userName").val();
            var reg = /^[a-zA-Z0-9]{1,10}$/;
            if (!reg.test(userName)) {
                layer.msg('用户名长度为1-10位', {icon: 2});
                return;
            }
            var password = $("#password").val();
            if (!reg.test(password)) {
                layer.msg('密码长度为1-10位', {icon: 2});
                return;
            }
            var timeRange = $("#config-demo").val();
            var phoneNum = $("#phoneNumText").val();
            //验证手机号
            if (!(/^1[34578]\d{9}$/.test(phoneNum))) {
                layer.msg("手机号码有误，请重填", {icon: 2});
                return ;
            }
            var startTime = dateChange(timeRange.split(' ~ ')[0]);
            var stopTime = dateChange(timeRange.split(' ~ ')[1]);
            if (userName === "") {
                layer.msg("账号不能为空", {icon: 2});
            } else if (password === "") {
                layer.msg("密码不能为空", {icon: 2});
            } else if (timeRange === "") {
                layer.msg("起止时间不能为空", {icon: 2});
            } else {
                var url = '/admin/modifyUserInfo';

                if (userNameOld === userName) {
                    var data = {
                        "id": userId,
                        // "username": userName,
                        "password": password,
                        "startTime": startTime,
                        "stopTime": stopTime,
                        "opusername": opusername,
                        "roleId": "1",
                        "phoneNum": phoneNum
                    };
                } else {
                    var data = {
                        "id": userId,
                        "username": userName,
                        "password": password,
                        "startTime": startTime,
                        "stopTime": stopTime,
                        "opusername": opusername,
                        "roleId": "1",
                        "phoneNum": phoneNum
                    };
                }

                commonAjaxPost(true, url, data, function (result) {
                    console.log(result)
                    if (result.code === "666") {
                        layer.msg("用户信息修改成功", {icon: 1});
                        setTimeout(function () {
                            window.location.href = 'TenantsManage.html';
                        }, 1000)
                        // $("#userTable").bootstrapTable('refresh');
                    } else if (result.code === "50003") {
                        //用户名已存在
                        layer.msg(result.message, {icon: 2});
                    } else if (result.code === "333") {
                        layer.msg(result.message, {icon: 2});
                        setTimeout(function () {
                            window.location.href = 'login.html';
                        }, 1000)
                    } else if (result.code === "20001") {
                        //用户有可用群组
                        layer.msg(result.message, {icon: 2});
                    } else {
                        layer.msg(result.message, {icon: 2})
                    }
                })
            }
        });
    }

}

// 获取所有用户角色
/* function getUserRole() {
    var url = '/admin/queryRoleInfo';
    $.ajax({
        "async": false,
        "url": httpRequestUrl + url,
        "type": "POST",
        // "data": JSON.stringify(data),
        "dataType": "json",
        "contentType": "application/json",
        success: function (result) {
            //console.log(result)
            var belongType = document.getElementById('userRoleAll');
            belongType.options.length = 0;
            for (var i = 0; i < result.data.length; i++) {
                var collOpt = document.createElement('option');
                collOpt.innerText = result.data[i].role;
                collOpt.value = result.data[i].id;
                belongType.appendChild(collOpt);
            }
        }
    });
} */

// 创建时间区域选择
function createTimePicker() {
    var beginTimeStore = '';
    var endTimeStore = '';
    var nowTime = getFormatDateSecond();
    var date = new Date();
    var milliseconds = date.getTime() + 1000 * 60 * 60 * 24 * 30;                                    //n代表天数,加号表示未来n天的此刻时间,减号表示过去n天的此刻时间   n:7
    var newDate = new Date(milliseconds);
    var dateAfterNow = timeFormat(newDate);

    $('#config-demo').daterangepicker({
        "minDate": nowTime,
        "startDate": nowTime,
        "endDate": dateAfterNow,
        "timePicker": true,
        "timePicker24Hour": true,
        "linkedCalendars": false,
        // "autoUpdateInput": false,
        "locale": {
            "resetLabel": "重置",
            "format": 'YYYY/MM/DD HH:mm:ss',
            "separator": " ~ ",//
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "起始时间",
            "toLabel": "结束时间'",
            "customRangeLabel": "自定义",
            "weekLabel": "W",
            "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
            "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月",
                "十一月", "十二月"],
            "firstDay": 1
        },
    }, function (start, end, label) {
        // beginTimeStore = start;
        // endTimeStore = end;
        // //console.log(this.startDate.format(this.locale.format));
        // //console.log(this.endDate.format(this.locale.format));
        // if (!this.startDate) {
        //     this.element.val('');
        // } else {
        //     this.element.val(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
        // }
    });

}
